<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_lap_lulus extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.kd_prodi';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$PMBPeriodID = isset($_POST['PMBPeriodID']) ? strval($_POST['PMBPeriodID']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.PMBPeriodID, a.no_pendaftaran, a.nama, a.alamat, a.no_telp, c.nama_fakultas, b.nama_prodi, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.status');
			$this->db->from('pmb_seleksi a');
			$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
			$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
			
			if($kd_fakultas != ''){
				$this->db->where('b.kd_fakultas', $kd_fakultas);
			}
			if($kd_prodi != ''){
				$this->db->where('a.kd_prodi', $kd_prodi);
			}
			if($PMBPeriodID != ''){
				$this->db->where('a.PMBPeriodID', $PMBPeriodID);
			}
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
		
		
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	/*function get_pdf(){
		# get data
		$this->db->flush_cache();
		$this->db->select('a.PMBPeriodID, a.no_pendaftaran, a.nama, a.alamat, a.no_telp, c.nama_fakultas, b.nama_prodi, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.status');
		$this->db->from('pmb_seleksi a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Periode / Gelombang', 'width'=>20, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'No Pendaftaran', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama', 'width'=>35, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Alamat', 'width'=>35, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'No Telepon', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Prodi', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama Orang Tua', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Alamat orang Tua', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Pekerjaan', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
				
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				$row->PMBPeriodID,
				$row->no_pendaftaran,
				$row->nama,
				$row->alamat,
				$row->no_telp,
				$row->nama_prodi,
				$row->nama_ortu,
				$row->alamat_ortu,
				$row->pekerjaan
			));
			$i++;
		}
		
		return $pdfdata;
		
	}*/
	
	function get_pdf($fil){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
		$this->db->select('a.PMBPeriodID, a.no_pendaftaran, a.nama, a.alamat, a.no_telp, c.nama_fakultas, b.nama_prodi, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.status');
		$this->db->from('pmb_seleksi a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
					
			$this->db->where('a.kd_prodi', $fil['kd_prodi']);
			$this->db->where('a.PMBPeriodID', $fil['PMBPeriodID']);
			
			//$this->db->where('a.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
	
}

?>